python |
您所在的位置:网站首页 › hive 导出csv 科学计数法转字符串 › python |
很多问题都解决了这个问题,但似乎没有一个解决方案能够完全按照我的需要工作。 我有一个数据框,其中有两列数字,每列 10-20 位数字。这些实际上是 ID 号,我想将它们连接起来。看起来最好的方法是首先将值转换为字符串。 但是,当使用 .astype(str) 进行转换时,pandas保留科学记数法,不会飞。 我尝试过的事情: 尝试过:str() 中的 dtype arg ('str') 或转换器(使用 read_csv() ) 结果:df.dtypes仍然列出“对象”,并且值仍然显示在 sci 中。符号 尝试过:pd.set_option('display.float_format', lambda x: '%.0f' % x) 结果:在 df.head() 中显示良好,但在使用 + 运算符强制转换为字符串和连接时恢复为科学记数法 尝试过:强制转换为 int、str 或 str(int(x)) . 结果:当我用 int() 强制一个值时, int 起作用,但当我使用 astype(int) 时不会。使用.apply()与 int()抛出“以 10 为基数的无效文字 long()”错误。 这感觉应该非常简单,急于弄清楚我错过了什么。 最佳答案 您尝试设置的显示格式。您可以将 float 格式化为数据框中的字符串。 import numpy as np import pandas as pd import numpy as np将 pandas 导入为 pd df=pd.DataFrame(data={'a':np.random.randint(low=1,high=100,size=10)*1e20,'b':np.random.randint(low=1,high=100,size=10)*1e20}) df.apply(lambda x: '{0:20.0f}|{1:20.0f}'.format(x.a,x.b),axis=1) Out[34]: 0 9699999999999998951424|4600000000000000000000 1 300000000000000000000|2800000000000000000000 2 9400000000000000000000|9000000000000000000000 3 2100000000000000000000|4500000000000000000000 4 5900000000000000000000|4800000000000000000000 5 7700000000000000000000|6200000000000000000000 6 1600000000000000000000|8000000000000000000000 7 100000000000000000000|400000000000000000000 8 9699999999999998951424|8000000000000000000000 9 4500000000000000000000|3500000000000000000000关于python - 将 pandas 列转换为不带科学记数法的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428734/ |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |